// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package demo
{
    import flash.display.BitmapData;

    import starling.display.Image;
    import starling.display.Sprite;
    import starling.textures.Texture;
    import starling.textures.TextureSmoothing;

    public class StarlingDemoScene extends Sprite
    {
        private var mTopLeftImg:Image;
        private var mTopLeftImgTexture:Texture;

        private var mTopRightImg:Image;
        private var mTopRightImgTexture:Texture;

        private var mBottomLeftImg:Image;
        private var mBottomLeftImgTexture:Texture;

        private var mBottomRightImg:Image;
        private var mBottomRightImgTexture:Texture;

        private var mMiddleImg:Image;
        private var mMiddleImgTexture:Texture;

        private var mTopMiddleImg:Image;
        private var mTopMiddleImgTexture:Texture;

        private var mLeftMiddleImg:Image;
        private var mLeftMiddleImgTexture:Texture;

        private var mRightMiddleImg:Image;
        private var mRightMiddleImgTexture:Texture;

        private var mBottomMiddleImg:Image;
        private var mBottomMiddleImgTexture:Texture;

        private var mPanelWidth:int = 200;
        private var mPanelHeight:int = 200;

        public function StarlingDemoScene()
        {
            super();
        }

        //==================================
        //== Public Function
        //==================================

        public function refreshPanelWidthAndHeight(_width:int, _height:int):void
        {
            if (mTopLeftImg != null)
            {
                mPanelWidth = Math.max(mTopLeftImg.width + mTopRightImg.width, _width);
                mPanelHeight = Math.max(mTopLeftImg.height + mTopRightImg.height, _height);
                doRefreshDisplay();
            }
        }


        public function onBitmapTextureUpdate(_topLeft:BitmapData,
                                              _topRight:BitmapData,
                                              _bottomLeft:BitmapData,
                                              _bottomRight:BitmapData,
                                              _middle:BitmapData,
                                              _topMiddle:BitmapData,
                                              _leftMiddle:BitmapData,
                                              _rightMiddle:BitmapData,
                                              _bottomMiddle:BitmapData):void
        {
            //=============
            //==TopLeft
            //=============
            mTopLeftImg && mTopLeftImg.removeFromParent(true);
            mTopLeftImgTexture && mTopLeftImgTexture.dispose();

            mTopLeftImgTexture = Texture.fromBitmapData(_topLeft, false);
            mTopLeftImg = new Image(mTopLeftImgTexture);
            mTopLeftImg.smoothing = TextureSmoothing.NONE;

            this.addChild(mTopLeftImg);

            //=============
            //==TopRight
            //=============
            mTopRightImg && mTopRightImg.removeFromParent(true);
            mTopRightImgTexture && mTopRightImgTexture.dispose();

            mTopRightImgTexture = Texture.fromBitmapData(_topRight, false);
            mTopRightImg = new Image(mTopRightImgTexture);
            mTopRightImg.smoothing = TextureSmoothing.NONE;

            this.addChild(mTopRightImg);

            //=============
            //==BottomLeft
            //=============
            mBottomLeftImg && mBottomLeftImg.removeFromParent(true);
            mBottomLeftImgTexture && mBottomLeftImgTexture.dispose();

            mBottomLeftImgTexture = Texture.fromBitmapData(_bottomLeft, false);
            mBottomLeftImg = new Image(mBottomLeftImgTexture);
            mBottomLeftImg.smoothing = TextureSmoothing.NONE;

            this.addChild(mBottomLeftImg);

            //=============
            //==BottomRight
            //=============
            mBottomRightImg && mBottomRightImg.removeFromParent(true);
            mBottomRightImgTexture && mBottomRightImgTexture.dispose();

            mBottomRightImgTexture = Texture.fromBitmapData(_bottomRight, false);
            mBottomRightImg = new Image(mBottomRightImgTexture);
            mBottomRightImg.smoothing = TextureSmoothing.NONE;

            this.addChild(mBottomRightImg);

            //=============
            //==TopMiddle
            //=============
            mTopMiddleImg && mTopMiddleImg.removeFromParent(true);
            mTopMiddleImgTexture && mTopMiddleImgTexture.dispose();

            mTopMiddleImgTexture = Texture.fromBitmapData(_topMiddle, false);
            mTopMiddleImg = new Image(mTopMiddleImgTexture);
            mTopMiddleImg.smoothing = TextureSmoothing.NONE;

            this.addChild(mTopMiddleImg);

            //=============
            //==LeftMiddle
            //=============
            mLeftMiddleImg && mLeftMiddleImg.removeFromParent(true);
            mLeftMiddleImgTexture && mLeftMiddleImgTexture.dispose();

            mLeftMiddleImgTexture = Texture.fromBitmapData(_leftMiddle, false);
            mLeftMiddleImg = new Image(mLeftMiddleImgTexture);
            mLeftMiddleImg.smoothing = TextureSmoothing.NONE;

            this.addChild(mLeftMiddleImg);

            //=============
            //==RightMiddle
            //=============
            mRightMiddleImg && mRightMiddleImg.removeFromParent(true);
            mRightMiddleImgTexture && mRightMiddleImgTexture.dispose();

            mRightMiddleImgTexture = Texture.fromBitmapData(_rightMiddle, false);
            mRightMiddleImg = new Image(mRightMiddleImgTexture);
            mRightMiddleImg.smoothing = TextureSmoothing.NONE;

            this.addChild(mRightMiddleImg);

            //=============
            //==BottomMiddle
            //=============
            mBottomMiddleImg && mBottomMiddleImg.removeFromParent(true);
            mBottomMiddleImgTexture && mBottomMiddleImgTexture.dispose();

            mBottomMiddleImgTexture = Texture.fromBitmapData(_bottomMiddle, false);
            mBottomMiddleImg = new Image(mBottomMiddleImgTexture);
            mBottomMiddleImg.smoothing = TextureSmoothing.NONE;

            this.addChild(mBottomMiddleImg);

            //=============
            //==Middle
            //=============
            mMiddleImg && mMiddleImg.removeFromParent(true);
            mMiddleImgTexture && mMiddleImgTexture.dispose();

            mMiddleImgTexture = Texture.fromBitmapData(_middle, false);
            mMiddleImg = new Image(mMiddleImgTexture);
            mMiddleImg.smoothing = TextureSmoothing.NONE;

            this.addChild(mMiddleImg);

            doRefreshDisplay();
        }

        private function doRefreshDisplay():void
        {
            var middleHorizontalSize:int = Math.max(0, mPanelWidth - mTopLeftImg.width - mTopRightImg.width);
            var middleVerticalSize:int = Math.max(0, mPanelHeight - mTopLeftImg.height - mBottomLeftImg.height);

            //============
            //==TopLeft
            //============
            mTopLeftImg.x = mTopLeftImg.y = 20;


            //=============
            //==TopMiddle
            //=============
            mTopMiddleImg.x = mTopLeftImg.x + mTopLeftImg.width;
            mTopMiddleImg.y = mTopLeftImg.y;
            mTopMiddleImg.width = middleHorizontalSize;

            //==============
            //==TopRight
            //==============
            mTopRightImg.x = mTopLeftImg.x + mPanelWidth - mTopRightImg.width;
            mTopRightImg.y = mTopLeftImg.y;


            //==============
            //==LeftMiddle
            //==============
            mLeftMiddleImg.x = mTopLeftImg.x;
            mLeftMiddleImg.y = mTopLeftImg.y + mTopLeftImg.height;
            mLeftMiddleImg.height = middleVerticalSize;


            //==================================
            //==Middle 比理像素+2,并偏移(-1,-1)
            //==================================
            mMiddleImg.x = mTopLeftImg.x + mTopLeftImg.width - 1;
            mMiddleImg.y = mTopLeftImg.y + mTopLeftImg.height - 1;
            mMiddleImg.width = middleHorizontalSize + 2;
            mMiddleImg.height = middleVerticalSize + 2;


            //================
            //==RightMiddle
            //================
            mRightMiddleImg.x = mTopLeftImg.x + mPanelWidth - mRightMiddleImg.width;
            mRightMiddleImg.y = mLeftMiddleImg.y;
            mRightMiddleImg.height = middleVerticalSize;


            //================
            //==BottomLeft
            //================
            mBottomLeftImg.x = mTopLeftImg.x;
            mBottomLeftImg.y = mTopLeftImg.y + mPanelHeight - mBottomLeftImg.height;

            //================
            //==BottomMiddle
            //================
            mBottomMiddleImg.x = mTopMiddleImg.x;
            mBottomMiddleImg.y = mBottomLeftImg.y;
            mBottomMiddleImg.width = middleHorizontalSize;

            //===============
            //==BottomRight
            //===============
            mBottomRightImg.x = mTopRightImg.x;
            mBottomRightImg.y = mBottomLeftImg.y;

        }


    }
}
